using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;

namespace zevenseas.Community.Web.UI.WebControls.WebParts
{
    public class QuickAlertWebPart : zsWebPart, IWebEditable
    {
        private QuickAlert quickAlert;

        private string listName;
        private SPEventType changeType = SPEventType.All;
        private SPAlertFrequency alertFrequency = SPAlertFrequency.Immediate;
        private string createAlertText = "Subscribe";
        private string deleteAlertText = "Unsubscribe";

        [WebBrowsable(false), WebDisplayName("Create Alert Label")]
        [Personalizable(PersonalizationScope.Shared), SPWebCategoryName("Quick Alert")]
        [DefaultValue("Subscribe")]
        public string CreateAlertText
        {
            get { return createAlertText; }
            set { createAlertText = value; }
        }

        [WebBrowsable(true), WebDisplayName("Delete Alert Label")]
        [Personalizable(PersonalizationScope.Shared), SPWebCategoryName("Quick Alert")]
        [DefaultValue("Unsubscribe")]
        public string DeleteAlertText
        {
            get { return deleteAlertText; }
            set { deleteAlertText = value; }
        }

        [WebBrowsable(true), WebDisplayName("List Name")]
        [Personalizable(PersonalizationScope.Shared), SPWebCategoryName("Quick Alert")]
        [DefaultValue("")]
        public string ListName
        {
            get { return listName; }
            set { listName = value; }
        }

        [WebBrowsable(true), WebDisplayName("Change Type")]
        [Personalizable(PersonalizationScope.Shared), SPWebCategoryName("Quick Alert")]
        [DefaultValue(SPEventType.All)]
        public SPEventType ChangeType
        {
            get { return changeType; }
            set { changeType = value; }
        }

        [WebBrowsable(true), WebDisplayName("Frequency")]
        [Personalizable(PersonalizationScope.Shared), SPWebCategoryName("Quick Alert")]
        [DefaultValue(SPAlertFrequency.Immediate)]
        public SPAlertFrequency AlertFrequency
        {
            get { return alertFrequency; }
            set { alertFrequency = value; }
        }

        protected override void CreateChildControls()
        {
            if (string.IsNullOrEmpty(listName))
            {
                Literal configWebPart = new Literal();
                configWebPart.Text = "To configure this web part, <a href=" + GetToolPaneLink() +
                                     ">open the tool pane</a> and then select the List and Alert Settings";
                Controls.Add(configWebPart);
                return;
            }

            quickAlert = new QuickAlert();
            quickAlert.AlertFrequency = AlertFrequency;
            quickAlert.ChangeType = ChangeType;
            quickAlert.ListName = ListName;
            quickAlert.CreateAlertText = CreateAlertText;
            quickAlert.DeleteAlertText = DeleteAlertText;

            Controls.Add(quickAlert);

            base.CreateChildControls();
        }

        EditorPartCollection IWebEditable.CreateEditorParts()
        {
            List<EditorPart> editors = new List<EditorPart>();

            EditorPart quickAlertEditor = new QuickAlertEditor();
            quickAlertEditor.ID = "QuickAlertEditor" + this.ID;

            editors.Add(quickAlertEditor);
            return new EditorPartCollection(editors);
        }

        object IWebEditable.WebBrowsableObject
        {
            get { return this; }
        }

    }
}